﻿<%@ WebHandler Language="C#" Class="HandlerNews" %>

using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Newtonsoft.Json;

public class HandlerNews : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        string strconn = LinkToDatabase.strConn;/*App_Code->UserClass.cs*/
        string strcmd = "";
        string type = context.Request.Form["type"];
        
        using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(strconn))
        {
            switch (type)
            {
                //1=======================================================================
                case "1":
                    strcmd = @"select 
                                     [news_id] as nid
                                    ,[news_title] as title
                                    ,[news_content] as content
                                    ,CONVERT(varchar(10),[news_date],20) as date
                                    from [dbo].[news] 
                                    order by [news_date] Desc
                                    OFFSET @Num ROWS FETCH NEXT 10 ROWS ONLY   

                            ";
                    break;
                //2=======================================================================
                case "2":
                    strcmd = @"SELECT COUNT ([news_id]) as num
                                      FROM [dbo].[news] 
                                      WHERE[news_id] IS NOT NULL;
                            ";
                    break;
                //3=======================================================================
                case "3":
                    strcmd = @"SELECT  [news_id] as nid
                                    ,[news_title] as title
                                    ,[news_content] as content
                                    ,CONVERT(varchar(10),[news_date],20) as date
                                    from [dbo].[news]
                                    where news_id=@id
                            ";
                    break;
                //4=======================================================================
                case "4":
                    strcmd = @"select 
                                     [news_id] as nid
                                    ,[news_title] as title
                                    ,[news_content] as content
                                    ,CONVERT(varchar(10),[news_date],20) as date
                                    from [dbo].[news] 
                                    order by [news_date] Desc
                                    OFFSET @Num ROWS FETCH NEXT 6 ROWS ONLY   

                            ";
                    break;
                
           
            }
            using (System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter(strcmd, conn))
            {
                SqlParameter Result;
                Result = new SqlParameter("@Result", SqlDbType.Bit);
                Result.Direction = ParameterDirection.Output;
                switch (type)
                {
                    //1=======================================================================
                    case "1":
                        //input給參數

                        da.SelectCommand.Parameters.AddWithValue("@Num",(Convert.ToInt32(context.Request.Form["PageNum"]) - 1) * 10);
                        break;

                    //3=======================================================================
                    case "3":
                        //input給參數

                        da.SelectCommand.Parameters.AddWithValue("@id",(Convert.ToInt32(context.Request.Form["content"])));
                        break;
                    //1=======================================================================
                    case "4":
                        //input給參數

                        da.SelectCommand.Parameters.AddWithValue("@Num", (Convert.ToInt32(context.Request.Form["PageNum"]) - 1) * 10);
                        break;
                    
                }
                DataSet ds = new DataSet();
                switch (type)
                {
                    case "1":
                    case "2":
                    case "4":
                    case "3":
                        da.Fill(ds, "data");
                        context.Response.ContentType = "application/json";
                        context.Response.Write(JsonConvert.SerializeObject(ds));
                        break;
                    
                }
                conn.Close();
            }
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}